package com.netflix.client;

import java.util.Collection;

public class Utils {
    public static boolean isPresentAsCause(Throwable throwableToSearchIn,
            Collection<Class<? extends Throwable>> throwableToSearchFor) {
        // 10 层异常堆栈
        int infiniteLoopPreventionCounter = 10;
        // 如果当前异常往上追踪10层，可以找到匹配的异常，就返回 true
        while (throwableToSearchIn != null && infiniteLoopPreventionCounter > 0) {
            infiniteLoopPreventionCounter--;
            // 遍历给定的异常集合
            for (Class<? extends Throwable> c: throwableToSearchFor) {
                if (c.isAssignableFrom(throwableToSearchIn.getClass())) {
                    return true;
                }
            }
            // 找到上一层异常
            throwableToSearchIn = throwableToSearchIn.getCause();
        }
        return false;
    }
}
